package com.lwm.tx.program.dao.impl;


import com.lwm.jdbc.pojo.AccountDO;
import com.lwm.tx.program.dao.IAccountDAO;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/**
 * @author liweimin
 * @date 2021/6/11 14:51
 * @since
 */
public class AccountDAOImpl implements IAccountDAO {

    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public AccountDO selectAccountById(int id) {
        List<AccountDO> accountDOList = jdbcTemplate.query("select * from account where id = ?", new BeanPropertyRowMapper<AccountDO>(AccountDO.class), id);
        if (accountDOList == null || accountDOList.size() == 0) {
            return null;
        }
        return accountDOList.get(0);
    }

    @Override
    public AccountDO selectAccountByName(String name) {
        List<AccountDO> accountDOList = jdbcTemplate.query("select * from account where name = ?", new BeanPropertyRowMapper<AccountDO>(AccountDO.class), name);
        if (accountDOList == null || accountDOList.size() == 0) {
            return null;
        }
        return accountDOList.get(0);
    }

    @Override
    public int updateAccount(AccountDO accountDO) {
        return jdbcTemplate.update("update  account set name =?, money = ? where id = ?", accountDO.getName(), accountDO.getMoney(), accountDO.getId());
    }
}
